<?php
/**
 * 商品管理
 *
 *
 *
 * * @好商城 (c) 2015-2018 33HAO Inc. (http://www.33hao.com)
 * @license    http://www.33 hao.c om
 * @link       交流群号：138182377
 * @since      好商城提供技术支持 授权请购买shopnc授权
 */



defined('In33hao') or exit ('Access Invalid!');
class store_goods_selectControl extends BaseSellerControl {

    public function __construct() {
        parent::__construct();
        Language::read('member_store_goods_index');
    }

    /**
     * 自选商品
     */
    public function indexOp(){
        $mystore = Model('my_store')->where(array('store_id'=>$_SESSION['store_id']))->select();
        $goodsModel = Model('goods');
        $goodsId = "";
        foreach($mystore as $v){
              $goodsId.=$v['goods_id'].",";
        }
        $condition['goods_id'] = array('in',trim($goodsId,','));
        $goodslist = $goodsModel->getGoodsList($condition);
        Tpl::output('show_page', $goodsModel->showpage());
        Tpl::output('goodsList',$goodslist);
        Tpl::showpage("own_select.list");
    }

    /**
     * 删除自选
     */
    public function delgoodsOp(){
          $id = $_POST['id'];
          $res = Model('my_store')->where(array("goods_id"=>$id,'store_id'=>$_SESSION['store_id']))->delete();
          if($res){
               echo 1;
               die;
          }
          echo 0;
    }

    /**
     * 插入选择的商品
     */
    Public function save_goodsOp(){
          $selectIds = $_GET['selectIds'];
          $selectIdsArr = explode("|",trim($selectIds,'|'));
          $goods_model= model('goods');
          $insertedArr = [];
          foreach($selectIdsArr as $k=> $v){
               // 判断商品是否存在
               $goods = $goods_model->getGoodsInfo(array("goods_id"=>$v));
               // 判断本店是否已经自选
               $my_goods = Model('my_store')->where(array('goods_id'=>$v,'store_id'=>$_SESSION['store_id']))->select();
               if(!in_array($v,$insertedArr)){
                   if($goods && empty($my_goods)){
                         $data['goods_id'] = $v;
                         $data['store_id'] = $_SESSION['store_id'];
                         $res = Model('my_store')->insert($data);
                         if($res){
                               $insertedArr[] = $v;
                         }
                   }
               }
          }
          echo 1;
    }
}